var app = getApp();
Page({
  data: {
    array: [
      "京",
      "浙",
      "津",
      "皖",
      "沪",
      "闽",
      "渝",
      "赣",
      "港",
      "鲁",
      "澳",
      "豫",
      "蒙",
      "鄂",
      "新",
      "湘",
      "宁",
      "粤",
      "藏",
      "琼",
      "桂",
      "川",
      "蜀",
      "冀",
      "贵",
      "黔",
      "晋",
      "云",
      "滇",
      "辽",
      "陕",
      "吉",
      "甘",
      "陇",
      "黑",
      "青",
      "苏",
      "台"
    ],
    index: 2, //省份简写数组索引值
    proviceName: "津",
    date: "2016-09-01", //添加日期
    time: "12:01", //添加时间
    isAgree: true, //是否同意协议
    warnMsg: "错误提示", //错误提示信息
    CarNum: "", //车牌号码
    pinpai: ["请输入品牌", "奔驰", "宝马", "英菲尼迪"],
    pinpaiIndex: 1000, //品牌索引
    pinpaiIds: [], //品牌总id的数组
    pinpaiId: 0, //品牌id
    pinpaiName: "请选择品牌", //品牌名字
    chexi: [],
    chexiIndex: 1000,
    chexiIds: [],
    chexiId: 0, //车系id
    chexiName: "", //车系名字
    chexing: [],
    chexingIndex: 1000,
    chexingIds: [],
    chexingId: 0, //车型id
    chexingName: "", //车型名字
    ewm: "",
    licenseplate: "",
    device_name: "",
    isCheck: false, //是否是新能源车牌
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var date = new Date();
    var endTime = app.utils.timeToDate(date);
    this.setData({
      endTime: endTime,
      registerDate: endTime,
    });
    //移除车辆三级联动的缓存
    // wx.removeStorageSync("b_id");
    // wx.removeStorageSync("pinpaiName");
    // wx.removeStorageSync("t_id");
    // wx.removeStorageSync("chexiName");
    // wx.removeStorageSync("m_id");
    // wx.removeStorageSync("chexingName");
    var that = this;
    var u_id = wx.getStorageSync("u_id");
    this.setData({
      u_id: u_id
    });
    var url = "obd/car/selectCarop";
    var data = 0;
    var callback = res => {
      var pinpai = [];
      var pinpais = res.data;
      var pinpaiIds = [];
      var logos = [];
      for (var i = 0; i < pinpais.length; i++) {
        logos.push(pinpais[i].LOGO_PATH);
        pinpai.push(pinpais[i].NAME);
        pinpaiIds.push(pinpais[i].ID);
      }
      that.setData({
        logos: logos,
        pinpai: pinpai,
        pinpaiIds: pinpaiIds
      });
    };
    app.getRequest(url, data, callback);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 判断是否登录
    app.getUserInfoFun();
    this.setData({
      b_id: wx.getStorageSync("b_id"),
      t_id: wx.getStorageSync("t_id"),
      m_id: wx.getStorageSync("m_id"),
      pinpaiName: wx.getStorageSync("pinpaiName"),
      chexiName: wx.getStorageSync("chexiName"),
      chexingName: wx.getStorageSync("chexingName"),
      CarNum: wx.getStorageSync("CarNum")
    });
  },
  bindDevice: function (e) {
    this.setData({
      device_name: e.detail.value
    });
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value,
      proviceName: this.data.array[e.detail.value]
    });
  },
  //提交按钮点击事件
  showTopTips: function () {
    var carnum = this.data.CarNum;
    var UPCarNum = carnum.toLocaleUpperCase();
    // 车牌号码正则
    var express = /^[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
    var that = this;
    if (!express.test(UPCarNum)) {
      this.setData({
        warnMsg: "车牌号码输入有误"
      });
      this.setData({
        showTopTips: true
      });
      setTimeout(function () {
        that.setData({
          showTopTips: false
        });
      }, 2000);
    } else {
      //ajax提交
      var that = this;
      var u_id = wx.getStorageSync("u_id");
      var logo = wx.getStorageSync("logo");
      var url = "api/car/addCar";
      var data = {
        u_id: u_id,
        b_id: this.data.b_id,
        t_id: this.data.t_id,
        m_id: this.data.m_id,
        b_name: this.data.pinpaiName,
        t_name: this.data.chexiName,
        m_name: this.data.chexingName,
        licenseplate: this.data.proviceName + UPCarNum,
        // device_name: this.data.device_name,
        logo: logo,
        // buycartime: this.data.registerDate, //购买时间
        licenseplate_type: this.data.isCheck ? 2 : 0
      };
      var callback = res => {
        if (res.code == 200) {
          //移除车辆三级联动的缓存
          wx.removeStorageSync("b_id");
          wx.removeStorageSync("pinpaiName");
          wx.removeStorageSync("t_id");
          wx.removeStorageSync("chexiName");
          wx.removeStorageSync("m_id");
          wx.removeStorageSync("chexingName");

          wx.setStorageSync("cartype_id", that.pinpaiId);
          that.setData({
            warnMsg: "添加成功"
          });
          that.setData({
            showTopTips: true
          });
          setTimeout(function () {
            that.setData({
              showTopTips: false
            });
          }, 2000);
          setTimeout(function () {
            wx.navigateBack({
              url: "/person/myCar/myCar"
            });
          }, 2000);
        } else if (res.code == 201) {
          that.setData({
            warnMsg: res.message
          });
          that.setData({
            showTopTips: true
          });
          setTimeout(function () {
            that.setData({
              showTopTips: false
            });
          }, 2000);
        }
      };
      app.getRequest(url, data, callback);
    }
  },
  bindCarInput: function (e) {
    this.setData({
      CarNum: (e.detail.value).toUpperCase().trim(),
    });
    wx.setStorageSync("CarNum", this.data.CarNum);
  },
  //购买日期时间选择
  bindTimeChange: function (e) {
    this.setData({
      registerDate: e.detail.value,
    })
  },
  //点击跳转到三级联动页面
  tolinkage: function () {
    wx.removeStorageSync('b_id');
    wx.removeStorageSync('t_id');
    wx.removeStorageSync('m_id');
    wx.removeStorageSync('pinpaiName');
    wx.removeStorageSync('chexiName');
    wx.removeStorageSync('chexingName');
    wx.navigateTo({
      url: "../linkage/linkage"
    });
  },
  //品牌的ajax
  bindPinpaiChange: function (e) {
    this.data.pinpaiName = this.data.pinpai[e.detail.value];
    this.setData({
      pinpaiIndex: e.detail.value,
      pinpaiId: this.data.pinpaiIds[e.detail.value],
      logo: this.data.logos[e.detail.value]
    });
    wx.setStorageSync(
      "defaultCar_img",
      app.globalData.globalUrl + this.data.logo
    );
    var thisID = this.data.pinpaiIds[e.detail.value];
    thisID = thisID - 0;
    var that = this;
    var url = "obd/car/selectCarop";
    var data = {
      p_id: thisID
    };
    var callback = res => {
      var chexi = [];
      var chexis = res.data;
      var chexiIds = [];
      for (var i = 0; i < chexis.length; i++) {
        chexi.push(chexis[i].NAME);
        chexiIds.push(chexis[i].ID);
      }
      that.setData({
        chexi: chexi,
        chexiIds: chexiIds
      });
    };
    app.getRequest(url, data, callback);
  },

  //车系的ajax
  bindChexiChange: function (e) {
    this.data.chexiName = this.data.chexi[e.detail.value];
    this.setData({
      chexiIndex: e.detail.value,
      chexiId: this.data.chexiIds[e.detail.value]
    });
    var thisID = this.data.chexiIds[e.detail.value];
    thisID = thisID - 0;
    var that = this;
    var url = "obd/car/selectCarop";
    var data = {
      p_id: thisID
    };
    var callback = res => {
      var chexing = [];
      var chexings = res.data;
      var chexingIds = [];
      for (var i = 0; i < chexings.length; i++) {
        chexing.push(chexings[i].NAME);
        chexingIds.push(chexings[i].ID);
      }
      that.setData({
        chexing: chexing,
        chexingIds: chexingIds
      });
    };
    app.getRequest(url, data, callback);
  },

  //车型的ajax
  bindChexingChange: function (e) {
    this.setData({
      chexingIndex: e.detail.value,
      chexingId: this.data.chexingIds[e.detail.value],
      chexingName: this.data.chexing[e.detail.value]
    });
  },
  //扫码的ajax
  sm: function () {
    //扫码
    wx.scanCode({
      onlyFromCamera: true,
      success: res => {
        // success
        this.setData({
          device_name: res.result
        });
        wx.showToast({
          title: "成功",
          icon: "seccess",
          duration: 1000
        });
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    });
  },
  isCheck: function () {
    this.setData({
      isCheck: !this.data.isCheck,
    })
  }

});